import Vue from "vue";
import VueRouter from 'vue-router'
 Vue.use(VueRouter)
 
 const router = new VueRouter({
    routes:[
        {
            path:'/',redirect:'/home'
        },
        {
            path:'/login',
            component:()=>import('../view/login.vue')
        },
        {
            path:'/home',
            component:()=>import('../view/home.vue'),
            meta:{
              keepAlive:true
            }
        },
        {
          path:'/ceshi',
          component:()=>import('../view/ceshi.vue')
      },
      {
        path:'/map',
        component:()=>import('../view/mapHome.vue')
      },
      {
        path:'/pdf',
        component:()=>import('../view/pdfHome.vue')
      },
      {
        path:'/docInfo',
        component:()=>import('../view/docInfo.vue')
      },
      {
        path:'/tMap',
        component:()=>import('../view/paragraph.vue'),
         meta:{
              keepAlive:true
            }
      }
    ],
    mode:'history',
    scrollBehavior: (to, from, savedPosition) => {
      return { x: 0, y: to.meta.scrollTop || 0 }; //进入该页面时，用之前保存的滚动位置赋值 
  },
 })
// 注册全局前置导航守卫
router.beforeEach((to,from,next)=>{
    if (to.path === '/login') {//若跳转的路径为登陆页面
        next();
      } else {//请求的是其他路径:判断是否已经登陆过
        if (localStorage.getItem('userId')) {
          next();
        } else {//用户未登录
          next({//强制跳转到登录页面
            path: '/login',
            query: {//查询,重定向至login的完整路径
              redirect: to.fullPath
            }
          })
        }
      }
})

 export default router